#
# ComicViewOptions.py
# (c) 2008 Will Baker.
#

from PyQt4.QtCore import Qt, QPoint, QPointF

class ComicViewOptions:
    NoRotation    = 0
    RightRotation = 1
    LeftRotation  = 2
    
    NoScale       = 0
    FitAllScale   = 1
    FitWidthScale = 2
    
    """
    Options for viewing a Comic.
    """
    def __init__( self ):
        self.scroll = QPointF( 0, 0 )
        self.rotate = ComicViewOptions.NoRotation
        self.scale = ComicViewOptions.FitAllScale
        self.double = False
        self.refine = False
        
    def toggle_rotation( self ):
        self.rotate = (self.rotate + 1) % 3
        
    def toggle_scale( self ):
        self.scale = (self.scale + 1) % 3
        
    def toggle_double( self ):
        self.double = not self.double
        
    def get_size( self, image_size, target_size ):
        """
        Find the size that the comic should be displayed at.
        """
        size = image_size
        if self.scale == ComicViewOptions.FitAllScale:
            size.scale( target_size, Qt.KeepAspectRatio )
        elif self.scale == ComicViewOptions.FitWidthScale:
            size.scale( target_size.width(), size.height(), Qt.KeepAspectRatio )

        return size

    def get_scroll( self, image_size, target_size ):
        """
        Find the offset that should be applied due to the scroll values.
        """
        if self.scale == ComicViewOptions.FitAllScale:
            return QPoint( 0, 0 )
        else:
            max_x_scroll = max( image_size.width() - target_size.width(), 0 )
            max_y_scroll = max( image_size.height() - target_size.height(), 0 )
            
            x_scroll = self.scroll.x() * max_x_scroll
            y_scroll = self.scroll.y() * max_y_scroll
            
            return QPoint( x_scroll, y_scroll )
            
